package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/28 23:33
 */
@Api(tags = "C端订单相关接口")
@RequestMapping(value = "/user/order")
@Slf4j
@RestController(value = "userOrderController")
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 用户端提交订单
     *
     * @return
     */
    @ApiOperation(value = "提交订单")
    @PostMapping(value = "/submit")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("ordersSubmitDTO = {}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @return
     */
    @ApiOperation(value = "订单支付")
    @PutMapping(value = "/payment")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) {
        // 此处跳过微信支付核心逻辑，直接默认支付成功
        log.info("ordersPaymentDTO = {}", ordersPaymentDTO);
        orderService.paySuccess(ordersPaymentDTO);
        return Result.success(new OrderPaymentVO());
    }

    /**
     * 分页查询历史订单
     *
     * @return
     */
    @GetMapping(value = "/historyOrders")
    @ApiOperation(value = "分页查询历史订单")
    public Result<PageResult> pageQueryHistoryOrder(Integer page, Integer pageSize, Integer status) {
        log.info("page = {},pageSize = {},status = {}", page, pageSize, status);
        PageResult pageResult = orderService.pageQueryHistoryOrder(page, pageSize, status);
        return Result.success(pageResult);
    }

    /**
     * 根据订单id查询订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据订单id查询订单详情")
    @GetMapping(value = "/orderDetail/{id}")
    public Result<OrderVO> getOrderDetailByOrderId(@PathVariable(value = "id") Long id) {
        OrderVO orderVO = orderService.getOrderDetailByOrderId(id);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     *
     * @return
     */
    @ApiOperation(value = "取消订单")
    @PutMapping(value = "/cancel/{id}")
    public Result cancelOrder(@PathVariable(value = "id") Long id) {
        orderService.cancelOrder(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @ApiOperation(value = "再来一单")
    @PostMapping(value = "/repetition/{id}")
    public Result getOneAgain(@PathVariable(value = "id") Long id){
        log.info("orderId = {}",id);
        orderService.getOneAgain(id);
        return Result.success();
    }

    /**
     * 用户催单
     * @return
     */
    @GetMapping(value = "/reminder/{id}")
    @ApiOperation(value = "用户催单")
    public Result reminder(@PathVariable(value = "id") Long id){
        orderService.reminder(id);
        return Result.success();
    }
}
